package gui;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import java.awt.Color;
import javax.swing.JPanel;

import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.DefaultTreeCellRenderer;


import domein.DomeinController;
import domein.Models.WaarnemingenTableModel;

public class NavigatiePanel extends JPanel {

	private DomeinController domeinController;
	private JTree jTree;
	private domein.Models.WereldTreeModel wereldTreeModel;
	
	private DefaultMutableTreeNode node;
	private List<Object> observerLijst = new ArrayList<Object>();

	public NavigatiePanel(DomeinController domeinController) throws IllegalStateException, SQLException, ClassNotFoundException {
		super();
		this.domeinController = domeinController;
		initGUI();
	}
	
	private void initGUI() throws IllegalStateException, SQLException, ClassNotFoundException 
	{
		wereldTreeModel = new domein.Models.WereldTreeModel(domeinController);
		
		try 
		{
			this.setPreferredSize(new java.awt.Dimension(224, 600));
			this.setLayout(null);
			this.setBackground(new java.awt.Color(78, 101, 125));
			this.setSize(212, 490);
			{
				jTree = new JTree(wereldTreeModel);
				this.add(jTree);
				jTree.setBounds(12, 12, 200, 476);
				jTree.setBackground(new java.awt.Color(78, 101, 125));
				
		        DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();
		        renderer.setTextNonSelectionColor(new java.awt.Color(255, 255, 255));
		        renderer.setBackgroundNonSelectionColor(new java.awt.Color(78, 101, 125));
			
				jTree.addTreeSelectionListener(new TreeSelectionListener() {
					public void valueChanged(TreeSelectionEvent e) {
			    	node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
			        notifyObservers(node);
			       
					}
				});
				  
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public JTree getTree(){
		return jTree;
		
	}
	
	// Eigen observer (Cedric)
	public void addObserver(Object o){
		observerLijst.add(o);
	}
	
	public void notifyObservers(DefaultMutableTreeNode node){
		for(Object o: observerLijst){
			((OverzichtPanel)o).update(node);
		}
		
	}
	
	public void refreshJTreeColors(){
        DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) jTree.getCellRenderer();
        renderer.setTextNonSelectionColor(new java.awt.Color(255, 255, 255));
        renderer.setBackgroundNonSelectionColor(new java.awt.Color(78, 101, 125));
	}
	
}
